﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using System.Data;
using CRMBiz;
using Newtonsoft.Json;
using CRMWeb.Utilities.Context;

namespace CRMWeb.WebServices.AM
{
    /// <summary>
    /// Summary description for Repayment
    /// </summary>
    public class Repayment : BaseHandler
    {
        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();
            //e.Cancel = true;
            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        public void SearchRepaymentByLoanID(int LoanID)
        {
            try
            {
                DataTable dt = RepaymentHandler.SearchRepaymentByLoanID(LoanID);
                string strJson = JsonConvert.SerializeObject(dt);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void SearchArrearsByLoanID(int LoanID)
        {
            try
            {
                DataTable dt = RepaymentHandler.SearchArrearsByLoanID(LoanID);
                string strJson = JsonConvert.SerializeObject(dt);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void SearchBankDetailByLoanID(int LoanID)
        {
            try
            {
                DataTable dt = RepaymentHandler.SearchBankDetailByLoanID(LoanID);
                string strJson = JsonConvert.SerializeObject(dt);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        #region private method For Repayments

        public void AddRepayment(string Repayment, string Freq, string Method1, string NextPaymentDate, string TotalPaid, string ContractualMaturity, string ProjectedMaturity, string Residual, string Balance, string DailyRate, string LoanID)
        {
            try
            {
                int RepaymentID = RepaymentHandler.AddRepayment(Repayment, Freq, Method1, NextPaymentDate, TotalPaid, ContractualMaturity, ProjectedMaturity, Residual, Balance, DailyRate, LoanID);
                context.Response.Write(RepaymentID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void UpdateRepayment(string Repayment, string Freq, string Method1, string NextPaymentDate, string TotalPaid, string ContractualMaturity, string ProjectedMaturity, string Residual, string Balance, string DailyRate, string LoanID)
        {
            try
            {
                int RepaymentID = RepaymentHandler.UpdateRepayment(Repayment, Freq, Method1, NextPaymentDate, TotalPaid, ContractualMaturity, ProjectedMaturity, Residual, Balance, DailyRate, LoanID);
                context.Response.Write(RepaymentID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        #endregion

        #region private method For Arrears

        public void AddArrears(string NumofDishonours, string TotalArrears, string DaysOverdue, string DateofDefault, string LoanID)
        {
            try
            {
                int RepaymentID = RepaymentHandler.AddArrears(NumofDishonours, TotalArrears, DaysOverdue, DateofDefault, LoanID);
                context.Response.Write(RepaymentID);
            }
            catch (Exception ex)  
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void UpdateArrears(string NumofDishonours, string TotalArrears, string DaysOverdue, string DateofDefault, string LoanID)
        {
            try
            {
                int RepaymentID = RepaymentHandler.UpdateArrears(NumofDishonours, TotalArrears, DaysOverdue, DateofDefault, LoanID);
                context.Response.Write(RepaymentID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        #endregion   UpdateBankInfo

        #region private method For BankInfo

        public void AddBankInfo(string BankName, string AccountNo, string BSB, int LoanID, string LoanType, string AccountHolder)
        {
            try
            {
                int intretvalue = RepaymentHandler.AddBankInfo(BankName, AccountNo, BSB, LoanID, LoanType, AccountHolder);
                context.Response.Write(intretvalue);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void UpdateBankInfo(string BankName, string AccountNo, string BSB, int LoanID, string LoanType, string AccountHolder)
        {
            try
            {
                int intretvalue = RepaymentHandler.UpdateBankInfo(BankName, AccountNo, BSB, LoanID, LoanType, AccountHolder);
                context.Response.Write(intretvalue);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        #endregion

        #region private method For PaymentSchedule

        public void RePaymentScheduleSelect()
        {
            string strJson = null;
            try
            {
                int LoanID = string.IsNullOrEmpty(context.Request.QueryString["LoanID"]) ? 0 : int.Parse(context.Request.QueryString["LoanID"]);
                DataTable dt = RepaymentHandler.DisplayPaymentSchedule(LoanID);
                strJson = JsonConvert.SerializeObject(dt);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public int PaymentScheduleAdd(string PaymentSchedule, string Amount, string LoanID)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = RepaymentHandler.AddPaymentSchedule(PaymentSchedule, Amount, LoanID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public int PaymentScheduleEdit(int paymentscheduleid, string PaymentSchedule, string Amount, string Balance)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = RepaymentHandler.EditPaymentSchedule(paymentscheduleid, PaymentSchedule, Amount, Balance);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public bool PaymentScheduleDelete(int paymentscheduleid)
        {
            bool flag = false;
            try
            {
                flag = RepaymentHandler.DeletePaymentSchedule(paymentscheduleid);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

        #endregion
    }
}